package model

type UserRequest struct {
	ID       uint   `json:"id"`                                            // 主键
	Username string `json:"username"`                                      // 用户名
	Email    string `json:"email" v:"email@required|email#邮箱不能为空|邮箱格式不正确"` // 邮箱
	Avatar   string `json:"avatar"`                                        // 头像
	Status   uint   `json:"status"`                                        // 状态
}

type UserResponse struct {
	ID         uint   `json:"id"`         // 主键
	Username   string `json:"username"`   // 用户名
	Email      string `json:"email"`      // 邮箱
	Avatar     string `json:"avatar"`     // 头像
	CreateTime string `json:"createTime"` // 创建时间
	UpdateTime string `json:"updateTime"` // 更新时间
	DeleteTime string `json:"deleteTime"` // 删除时间
	Status     uint   `json:"status"`     // 状态
}

type UserResponsePage struct {
	List []UserResponse `json:"datalist"`
	Page Page           `json:"page"`
}

func (userRequest *UserRequest) Convert() User {
	user := User{}
	user.Id = userRequest.ID
	user.Username = userRequest.Username
	user.Email = userRequest.Email
	user.Avatar = userRequest.Avatar
	user.Status = userRequest.Status
	return user
}

func (*UserResponse) Convert(user *User) *UserResponse {
	return &UserResponse{
		ID:         user.Id,
		Username:   user.Username,
		Email:      user.Email,
		Avatar:     user.Avatar,
		CreateTime: user.CreateTime.String(),
		UpdateTime: user.UpdateTime.String(),
		DeleteTime: user.DeleteTime.String(),
		Status:     user.Status,
	}
}

func (User) Convert(user []User) []UserResponse {
	userResponse := make([]UserResponse, len(user))
	for i := range user {
		userResponse[i] = *userResponse[i].Convert(&user[i])
	}
	return userResponse
}

func (User) ConvertPage(user []User, page Page) UserResponsePage {
	userResponse := make([]UserResponse, len(user))
	for i := range user {
		userResponse[i] = *userResponse[i].Convert(&user[i])
	}
	return UserResponsePage{
		List: userResponse,
		Page: page,
	}
}
